/*
 * Created by JFormDesigner on Fri Nov 22 16:33:43 CST 2024
 */

package   com.hafu.view.RuKu;
import com.hafu.model.GysInfo;
import     com.hafu.model.RuKuDetail;
import com.hafu.model.RuKuMain;
import com.hafu.model.SpInfo;
import    com.hafu.service.GysInfoService;
import   com.hafu.service.RuKuService;
import    com.hafu.service.SpInfoService;
import com.hafu.util.StringUtil;
import java.awt.*;
import java.awt.event.*;
import java.math.BigDecimal; import java.util.ArrayList;
import java.util.Vector; import javax.swing.*;
import javax.swing.GroupLayout; import javax.swing.table.*;
/**
 * @author qw
 */
public class AddRuKuFrm extends JFrame {
    private GysInfoService gysInfoService = new GysInfoService();
    private SpInfoService spInfoService = new SpInfoService();
    private RuKuService ruKuService = new RuKuService();
    private TableColumn tableColumn;

    public AddRuKuFrm() {
        initComponents();
        fillComboBox();

    }

    private void fillComboBox() {
        ArrayList<GysInfo> gysInfoByName = gysInfoService.getGysInfoByName(new GysInfo());
        for (GysInfo gysInfo : gysInfoByName) {
            comboBox1.addItem(gysInfo.getName());
        }
        //表格第一列的下拉框
        JComboBox<SpInfo> comboBox = new JComboBox<>();
        ArrayList<SpInfo> spInfoByName = spInfoService.getSpInfoByName(new SpInfo());
        for (SpInfo spInfo : spInfoByName) {
            comboBox.addItem(spInfo);
        }

        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        tableColumn = table1.getColumnModel().getColumn(0);
        tableColumn.setCellEditor(new DefaultCellEditor(comboBox));
    }


    private void comboBox1(ActionEvent e) {
        // TODO add your code here
    }

    private void button3(ActionEvent e) {
        // TODO add your code here
    }

    private void button1(ActionEvent e) {
        // TODO add your code here 给表格添加新行
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        defaultTableModel.addRow(new Vector());
    }

    private void button2(ActionEvent e) {
        // TODO add your code here 提交数据
        RuKuMain ruKuMain = new RuKuMain();
        ruKuMain.setRukuName(textField2.getText());
        ruKuMain.setGysName(comboBox1.getSelectedItem().toString());
        //遍历表格的每一行
        DefaultTableModel defaultTableModel = (DefaultTableModel) table1.getModel();
        int rowCount = defaultTableModel.getRowCount();
        ArrayList<RuKuDetail> rukudetailList = new ArrayList<>();
        BigDecimal totalPrice = new BigDecimal(0);
        for (int i = 0; i < rowCount; i++) {
            SpInfo SpInfo1 = (SpInfo) table1.getValueAt(i, 0);
            if (SpInfo1 == null) {
                continue;
            }
            if (StringUtil.isEmpty(SpInfo1.getName())) {
                continue;
            }
            String s = table1.getValueAt(i, 1).toString();
            Integer num = Integer.parseInt(s);
            RuKuDetail ruKuDetail = new RuKuDetail();
            ruKuDetail.setSpName(SpInfo1.getName());
            ruKuDetail.setNum(num);
            ruKuDetail.setPrice(SpInfo1.getPrice());
            rukudetailList.add(ruKuDetail);
            totalPrice = totalPrice.add(SpInfo1.getPrice().multiply(new BigDecimal(num)));
        }
        ruKuMain.setRukudetailList(rukudetailList);
        ruKuMain.setPrice(totalPrice);
        boolean b = ruKuService.addRuKu(ruKuMain);
        if (b){
            JOptionPane.showMessageDialog(null,"添加成功");
            this.dispose();
        }else {
            JOptionPane.showMessageDialog(null,"添加失败");

        }

}

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        comboBox1 = new JComboBox();
        button1 = new JButton();
        button2 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label1 = new JLabel();
        label2 = new JLabel();
        textField2 = new JTextField();

        //======== this ========
        setTitle("\u6dfb\u52a0\u5165\u5e93\u5355");
        Container contentPane = getContentPane();

        //---- comboBox1 ----
        comboBox1.addActionListener(e -> comboBox1(e));

        //---- button1 ----
        button1.setText("\u6dfb\u52a0\u65b0\u884c");
        button1.addActionListener(e -> {
			button3(e);
			button1(e);
			button1(e);
		});

        //---- button2 ----
        button2.setText("\u63d0\u4ea4\u5165\u5e93");
        button2.addActionListener(e -> button2(e));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null},
                },
                new String[] {
                    "\u5546\u54c1", "\u6570\u91cf"
                }
            ));
            scrollPane1.setViewportView(table1);
        }

        //---- label1 ----
        label1.setText("\u4f9b\u5e94\u5546\u9009\u62e9");

        //---- label2 ----
        label2.setText("\u5165\u5e93\u540d\u79f0");

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(58, 58, 58)
                    .addComponent(label2)
                    .addGap(47, 47, 47)
                    .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 142, Short.MAX_VALUE)
                    .addComponent(label1)
                    .addGap(42, 42, 42)
                    .addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addGap(58, 58, 58)
                    .addComponent(button1)
                    .addGap(75, 75, 75)
                    .addComponent(button2)
                    .addGap(61, 61, 61))
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(0, 142, Short.MAX_VALUE)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 632, GroupLayout.PREFERRED_SIZE)
                    .addGap(134, 134, 134))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(31, 31, 31)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(button2)
                            .addComponent(button1)
                            .addComponent(comboBox1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addComponent(label1))
                        .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(label2)
                            .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 303, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(84, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JComboBox comboBox1;
    private JButton button1;
    private JButton button2;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label1;
    private JLabel label2;
    private JTextField textField2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
